package com.polaris.live.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.blankj.utilcode.util.StringUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.polaris.live.common.adapter.BaseAdapter
import com.polaris.live.common.ext.dp
import com.polaris.live.databinding.AdapterActCenterBinding
import com.polaris.live.databinding.AdapterActCenterTitleBinding
import com.polaris.live.resp.back_resp.ActCenterBaseVo
import com.polaris.live.translation.R.string
import com.polaris.live.ui.user.ActCenterActivity
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.widget.refresh.BaseBindingViewHolder

/**
 * 活动中心适配器
 *
 * @author Created by 半仙 on 2024/7/29/029 15:51
 */
class ActCenterAdapter : BaseAdapter<Any, BaseViewHolder>(0) {

    override fun getDefItemViewType(position: Int): Int {
        return when (val item = data[position]) {
            is ActCenterBaseVo -> TYPE_DATA
            is Int -> TYPE_TITLE
            else -> throw IllegalArgumentException("Unknown item type: $item")
        }
    }

    override fun onCreateDefViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): BaseBindingViewHolder<*, *> {
        val layoutInflater = LayoutInflater.from(parent.context)
        return when (viewType) {
            TYPE_DATA -> ActActivityViewHolder(
                AdapterActCenterBinding.inflate(layoutInflater, parent, false)
            )

            TYPE_TITLE -> ActActivityTitleViewHolder(
                AdapterActCenterTitleBinding.inflate(layoutInflater, parent, false)
            )

            else -> throw IllegalArgumentException("Invalid view type: $viewType")
        }
    }

    class ActActivityViewHolder(mBinding: AdapterActCenterBinding) :
        BaseBindingViewHolder<AdapterActCenterBinding, ActCenterBaseVo>(mBinding) {
        override fun bind(item: ActCenterBaseVo) {
            Glide.with(mBinding.root.context).load(ImageUtils.imgPath(item.banner))
                .transform(RoundedCorners(8.dp)).into(mBinding.ivBanner)
            if (item.processing) {
                mBinding.tvTypeTip.text = item.typeName
                mBinding.tvTypeTip.visibility = View.VISIBLE
            } else {
                mBinding.tvTypeTip.visibility = View.GONE
            }
        }
    }

    class ActActivityTitleViewHolder(mBinding: AdapterActCenterTitleBinding) :
        BaseBindingViewHolder<AdapterActCenterTitleBinding, Int>(mBinding) {
        override fun bind(item: Int) {
            mBinding.tvTitleText.text = when (item) {
                ActCenterActivity.ENDED -> StringUtils.getString(string.over_string)
                ActCenterActivity.PROCESSING -> StringUtils.getString(string.in_progress)
                ActCenterActivity.NOTSTART -> StringUtils.getString(string.coming_soon)
                else -> ""
            }
        }

    }

    override fun convert(holder: BaseViewHolder, item: Any) {
        when (holder) {
            is ActActivityViewHolder -> if (item is ActCenterBaseVo) holder.bind(item)
            is ActActivityTitleViewHolder -> if (item is Int) holder.bind(item)
        }
    }

    companion object {
        const val TYPE_TITLE = 1
        const val TYPE_DATA = 2
    }

}